"""
@name : 12.元类
@author : wenyao
@projectname: sanchuanglianxi
"""
# class A:
#     pass

# class Animal():
#     def __init__(self,name):
#         self.name = name
#     def eat(self):
#         print("i am eating....")
# class Dog(Animal):
#     def eat(self):
#         print("dog is eating.....")

# Dog = type("Dog",(Animal,),{"eat":eat})

# dog = Dog(Animal)
#面向对象里有两种关系
#1、继承关系，父类与子类关系   继承关系中 object是所有类的父类，最顶层的基类。
#2、实例与类的关系，  type就是最顶层的类，就是元类。
# print(type(dog))
# print(type(Dog))
# print(type(type))
# print(type(object))
# #object = type
# print(Dog.__bases__)
# print(Animal.__bases__)
# print(type.__bases__)
#object = type()
#class type(object):
#type object  相当于鸡生蛋 先有鸡还是先有蛋


#通过元类去创建
# def init(self, name):
#     self.name = name
# def eat(self):
#     print("i am eating....")
# def dog_eat(self):
#     print("dog is eating.....")
#
# Animal = type("Animal", (object,), {"__init__": init, "eat": eat})
# a = Animal("wen")
# a.eat()
# print(type(Animal))
# #     def eat(self):
# #         print("dog is eating.....")
#
# Dog1 = type("Dog",(Animal,),{"eat":dog_eat})
# dog = Dog1("wen")
# #print(Dog1.__metaclass__)
# #dog = Dog("wen")
# dog.eat()

#编写元类
class MyMeta(type):
    def __init__(self,*args):
        print("test my metaclass")
        print(args)

    def __new__(cls,*args):
        print("test __new__ of my metaclass")
        return type.__new__(cls,*args)

class Foo(object,metaclass=MyMeta):
    #__metaclass__ = MyMeta
    def __init__(self):
        print("Foo __init__")

print(Foo.__bases__)

#元类可以规定类创建时候的一些行为
# class MyMate(type):
#     def __new__(cls,name, bases, attrs):
#         if "foo" not in attrs:
#             raise TypeError("必须设置一个foo属性")
#         attrs["test"] = "my mate test"
#         return type.__new__(cls,name,bases,attrs)
#
# class A(metaclass=MyMate):
#     foo = 1
#
# a = A()
# print(a.test)
# class B(metaclass=MyMate):
#     pass